<?php
namespace app\admin\controller;

use app\_interface\ResourceInterface;
use app\common\entity\ResponseData;
use think\Exception;
use think\facade\Request;

class File extends AdminBase implements ResourceInterface
{
    public function list()
    {
        $where = input('where');
        $key = input('query', "");
        $model = \app\model\File::when(isset($where['name']), fn($query) => $query->scope('name', $where['name']))
            ->when($key, fn($query) => $query->whereLike('name', '%'.$key.'%'))
            ->page(input('page', 1), input('size', 10));
        $count = $model->count();
        $files = $model->select();
        return ResponseData::success($files, ['sum' => $count, 'page' => input('page', 1), 'size' => input('size', 10)]);
    }

    public function detail()
    {
        $id = input('id', 0);
        $file = \app\model\File::find($id);
        $file = $file->toArray();
        $file['file'] = $file['url'];
        return ResponseData::success($file);
    }

    public function add()
    {
        $data = Request::only(['name', 'url', 'hash', 'path', 'status']);
        $file = \app\model\File::create($data);
        return ResponseData::success($file);
    }

    public function update()
    {
        $id = input('id');
        $file = \app\model\File::find($id);
        $file->allowField(['url', 'path', 'hash', 'status'])->save(input());
        return ResponseData::success($file);
    }

    public function delete($id)
    {
        $id = input('id');
        if (!$id) {
            throw new Exception('非法请求');
        }
        $res = (new \app\model\File)->where('id', '=', $id)->delete();
        return ResponseData::success($res);
    }

    public function getListColumns()
    {
        $list = new \Sffi\ElementPlus\ElList();
        $list->addColumn(new \Sffi\ElementPlus\ElList\Column('文件名称', 'name', 'name'))
            ->addColumn(new \Sffi\ElementPlus\ElList\ElImage('文件URL', 'url', 'url'))
            ->addColumn(new \Sffi\ElementPlus\ElList\Column('文件path', 'path', 'path'))
            ->addColumn(new \Sffi\ElementPlus\ElList\Column('文件哈希值', 'hash', 'hash'))
            ->addColumn(new \Sffi\ElementPlus\ElList\Column('创建时间', 'create_time', 'create_time'))
            ->addColumn(new \Sffi\ElementPlus\ElList\Column('文件状态', 'status', 'status_text'));
            $list->addSearch(new \Sffi\ElementPlus\ElSearch\ElInput('name','name', '请输入文件名称', 0, 31));
        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns()
    {
        $form = new \Sffi\ElementPlus\ElForm();
        $file = new \Sffi\ElementPlus\ElForm\ElFile('文件', 'file', false);
        $file->setFileType('image')
            ->setSaveType('local', url('/admin/upload/local',[],false, true)->build())
            ->setHeaders([
                'V-Time'=>time() * 1000,
                'V-Token'=>$this->request->header('V-Token'),
                'V-Uid' => $this->request->header('V-Uid'),
            ]);
        $form->addColumn($file)
             ->addColumn(new \Sffi\ElementPlus\ElForm\ElInput('文件名称', 'name', '请填写文件名'))
             ->addColumn(new \Sffi\ElementPlus\ElForm\ElInput('文件URL', 'url', '请填写文件URL'))
             ->addColumn(new \Sffi\ElementPlus\ElForm\ElInput('文件哈希值', 'hash', '请填写文件哈希值'));
        $rule = [
            'name' => [
                ['require' => true, 'message' => '请填写文件名称', 'trigger' => 'blur'],
            ],
            'hash' => [
                ['require' => true, 'message' => '请填写文件哈希值', 'trigger' => 'blur'],
            ],
        ];
        return ResponseData::success($form->__toArray(), ['rule' => $rule]);
    }
}
